package com.wangzeqi.neihanduanzi.msg;



import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.wangzeqi.neihanduanzi.base.BaseActivity;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.discovery.DiscoveryActivity;

import com.wangzeqi.neihanduanzi.enter.EnterActivity;
import com.wangzeqi.neihanduanzi.enter.EnterPersonActivity;
import com.wangzeqi.neihanduanzi.entity.AllEntity;
import com.wangzeqi.neihanduanzi.home.HomeActivity;
import com.wangzeqi.neihanduanzi.home.jokes.AdapterJokes;
import com.wangzeqi.neihanduanzi.news.activity.NewsActivity;
import com.wangzeqi.neihanduanzi.singleton.QueueSingleton;
import com.wangzeqi.neihanduanzi.exit.StopMainActivity;


/**
 * Created by dllo on 16/1/7.
 */


public class MsgActivity extends BaseActivity implements View.OnClickListener {

    private ImageButton imageButton;
    private AdapterJokes adapter;
    private RecyclerView recyclerView;
    private AllEntity allEntity;
    private TextView textView;
    private String url = "http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-102";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.msg_activity);
        findViewById(R.id.rl_home_btn).setOnClickListener(this);
        findViewById(R.id.rl_discovery_btn).setOnClickListener(this);
        findViewById(R.id.rl_review_btn).setOnClickListener(this);
        findViewById(R.id.rl_msg_btn).setOnClickListener(this);
        findViewById(R.id.msg_tghd).setOnClickListener(this);
        findViewById(R.id.msg_xtxx).setOnClickListener(this);
        findViewById(R.id.msg_fsgz).setOnClickListener(this);
        textView = (TextView) findViewById(R.id.msg_top_right_btn);
        textView.setOnClickListener(this);
        imageButton = (ImageButton) findViewById(R.id.msg_top_left_id);
        imageButton.setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.rv_jokes);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AdapterJokes(this);
        recyclerView.setAdapter(adapter);
        addData();

    }
    private void addData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                allEntity = gson.fromJson(response, AllEntity.class);
                adapter.addData(allEntity);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(MsgActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        QueueSingleton.getInstance().getQueue().add(request);
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.rl_home_btn:
                Intent intentHome = new Intent(this, HomeActivity.class);
                startActivity(intentHome);
                finish();
                break;
            case R.id.rl_discovery_btn:
                Intent intentDiscovery = new Intent(this, DiscoveryActivity.class);
                startActivity(intentDiscovery);
                finish();
                break;
            case R.id.rl_review_btn:
                Intent intentReview = new Intent(this, NewsActivity.class);
                startActivity(intentReview);
                finish();
                break;
            case R.id.rl_msg_btn:

                Intent intentMsg = new Intent(this, MsgActivity.class);
                startActivity(intentMsg);
                break;
            case R.id.msg_tghd:
                    Intent intent = new Intent(this, EnterActivity.class);
                startActivity(intent);
                break;
            case R.id.msg_xtxx:
                Intent intent1 = new Intent(this, EnterActivity.class);
                startActivity(intent1);
                break;
            case R.id.msg_fsgz:
                Intent intent2 = new Intent(this, EnterActivity.class);
                startActivity(intent2);
                break;
            case R.id.msg_top_left_id:
                Intent intent4 = new Intent(this, EnterPersonActivity.class);
                startActivity(intent4);
                break;

            case R.id.msg_top_right_btn:
                Intent intent5 = new Intent(this, StopMainActivity.class);
                startActivity(intent5);
                finish();
                break;
        }
    }
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
